package com.ofsize.keepmoving;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.DragEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.ofsize.keepmoving.data.Garden;
import com.ofsize.keepmoving.drag.ImgDragShadowBuilder;
import com.squareup.okhttp.OkHttpClient;

import java.util.List;

/**
 * Created by Dengyong on 2015/6/3.
 */

public class MainActivity extends Activity {

    private List<String> urls;
    private RecyclerView recyclerView;
    private StaggeredImageAdapter staggeredImageAdapter;
    private TextView tvAppleCnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFresco(this);
        initRecyclerView();
        // 初始化苹果篮子
        initBasket();
    }

    /**
     * 初始化Fresco
     * @param context
     */
    private void initFresco(Context context) {
        OkHttpClient okHttpClient = new OkHttpClient();
        ImagePipelineConfig config = OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient)
        .build();
        Fresco.initialize(context, config);
    }

    /**
     * 初始化Recycler视图
     */
    private void initRecyclerView() {
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view_apples);
        // 获取苹果列表
        urls = Garden.getApples();
        staggeredImageAdapter = new StaggeredImageAdapter(this, urls);

        // 两列
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(staggeredImageAdapter);

        // 默认动画效果
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    /**
     * 初始化苹果篮子
     */
    private void initBasket() {
        View basket = findViewById(R.id.img_basket);
        basket.setOnDragListener(new View.OnDragListener() {
            public boolean onDrag(View v, DragEvent event) {
                if (event.getAction() == DragEvent.ACTION_DRAG_ENTERED) {
                    Animation shake = AnimationUtils.loadAnimation(v.getContext(), R.anim.shake);
                    v.startAnimation(shake);
                } else if (event.getAction() == DragEvent.ACTION_DROP) {
                    addToBasket();
                    v.invalidate();
                }
                return true;
            }
        });
        basket.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                emptyBasket();
                return true;
            }
        });
        tvAppleCnt = ((TextView) findViewById(R.id.tv_basket_apple_count));
    }

    /**
     * 添加苹果到篮子
     * @param v
     */
    private void addToBasket() {
        int count = 1;
        Object tag = tvAppleCnt.getTag();
        if (tag != null) {
           count = (int)tag + 1;
        }
        tvAppleCnt.setText("" + count);
        tvAppleCnt.setTag(count);
    }

    /**
     * 清空苹果篮子
     */
    private void emptyBasket() {
        tvAppleCnt.setText("");
        tvAppleCnt.setTag(null);
    }
}